package com.java.simple.smallspring.beans;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoujunlin
 * @date 2022年08月19日 15:47
 * @desc
 */
public class PropertyValues {

    private final List<PropertyValue> propertyValueList = new ArrayList<>();

    public void addPropertyValue(PropertyValue propertyValue) {
        for (int i = 0; i < this.propertyValueList.size(); i++) {
            PropertyValue currentPV = this.propertyValueList.get(i);
            if (currentPV.getName().equals(propertyValue.getName())) {
                // 覆盖原有的属性值 并直接return
                this.propertyValueList.set(i, propertyValue);
                return;
            }
        }
        // 新增
        this.propertyValueList.add(propertyValue);
    }

    public PropertyValue[] getPropertyValues() {
        return this.propertyValueList.toArray(new PropertyValue[0]);
    }

    public PropertyValue getPropertyValue(String propertyName) {
        for (PropertyValue propertyValue : this.propertyValueList) {
            if (propertyValue.getName().equals(propertyName)) {
                return propertyValue;
            }
        }
        return null;
    }

}
